package com.nju.edu.android_app.callback;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Bundle;
import android.os.Message;

import com.nju.edu.android_app.DetailActivity;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.net.RequestListener;

public class ShowListener implements RequestListener {
	
	private DetailActivity detailActivity;
	
	public ShowListener(DetailActivity detailActivity)
	{
		this.detailActivity=detailActivity;
	}

	@Override
	public void onComplete(String response) {
		// TODO Auto-generated method stub
		try {
			Message msg = new Message();
			Bundle bundle = new Bundle();
			JSONTokener jsonTokener = new JSONTokener(response);
			JSONObject weibo = (JSONObject) jsonTokener.nextValue();
			bundle.putString("text",weibo.getString("text"));
			bundle.putString("source", weibo.getString("source"));
			JSONObject user =weibo.getJSONObject("user");
			bundle.putString("name", user.getString("name"));
			bundle.putString("head_url", user.getString("profile_image_url"));
			if(weibo.has("bmiddle_pic"))
				bundle.putString("pic_url",
						weibo.getString("bmiddle_pic"));
			if(weibo.has("retweeted_status"))
			{
				JSONObject retweet =weibo.getJSONObject("retweeted_status");
				bundle.putString("retweet_text", retweet.getString("text"));
				JSONObject re_user =retweet.getJSONObject("user");
				bundle.putString("retweet_user",re_user.getString("name"));
			}
			msg.setData(bundle);
			detailActivity.handler.sendMessage(msg);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onIOException(IOException e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onError(WeiboException e) {
		// TODO Auto-generated method stub
		
	}

}
